package com.woniuxy.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.Result;


@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	
	//查询所有用户
	@GetMapping("/all")
	public List<User> findAllUser(){
		
		List<User> users = userService.findAllUser();
		
		for (User user : users) {
			ArrayList<String> rnames = new ArrayList<String>();
			int uid = user.getUid();
			List<Integer> rids = findRidByUid(uid);
			for (int rid : rids) {
				String rname = findRnameByRid(rid);
				rnames.add(rname);
			}
			user.setRoles(rnames);
		}
		
		return users;
		}
	//通过用户id查询用户id
	@GetMapping("/findridbyuid")
	public List<Integer> findRidByUid(int uid){
		return userService.findRidByUid(uid);
	}
	//通过角色id查询角色名称
	@GetMapping("/findpnamebypid")
	public String findRnameByRid(int rid){
		return userService.findRnameByRid(rid);
	}
	//根据状态查询用户
	@GetMapping("/findbystatus")
	public Result<Object> findUserByStatus(int status){
		Result<Object> result = new Result<>();
		if (status==1 || status==0) {
			List<User> users = userService.findUserByStatus(status);
			if(users==null) {
				result.setMessage("没有数据");
				return result;
			}
			result.setData(users);
			return result;
		}
		result.setMessage("异常");
		return result;
	}
	//根据关键字模糊查询用户
		@GetMapping("/findbywords")
		public Result<Object> findUserByWords(String words){
			System.out.println("|||||||||||||||||||||||||"+words);
			Result<Object> result = new Result<>();
			List<User> users = userService.findUserByWords(words);
			if(users==null) {
				result.setMessage("没有数据");
				return result;
			}
			result.setData(users);
			return result;
		}
	//新增用户--------------------存疑
	@GetMapping("/insert")
	public int insertUser() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String ctime = sdf.format(date);
		User user = new User().setUname("小张童鞋").setAccount("zhang").setPwd("123").setStatus(1).setCtime(ctime);
		userService.insertUser(user);
		return 0;
	}
	//通过rid查找用户
	public User findUserByUid(int uid) {
		return userService.findUserByUid(uid);
	}
	//登录
	@RequestMapping("/login")
	public Result<Object> login(String account,String pwd) {

		//根据账户查询User
		User user = userService.findUserByAccount(account);
		//验证验证码
		//验证密码
		if (user==null) {
			System.out.println("不存在的账号");
			Result<Object> result = new Result<>();
			result.setMessage("不存在的账号");
			return result;
		}
		if(!user.getPwd().equals(pwd)) {
			System.out.println("密码错误");
			Result<Object> result = new Result<>();
			result.setMessage("密码错误");
			return result;
		}
		System.out.println("登录成功");
		Result<Object> result = new Result<>();
		result.setMessage("登录成功");
		return result;
	}
	//修改密码
	@GetMapping("/changepwd")
	public Result<Object> changePwd(String oldpwd,String newpwd,String repwd,String account) {
		User user = userService.findUserByAccount(account);
		//用oldpwd和user.pwd对比
		if(!user.getPwd().equals(oldpwd)) {
			//旧密码输入不正确
			System.out.println("密码错误");
			Result<Object> result = new Result<>();
			result.setMessage("密码错误");
			return result;
		}
		else if (!newpwd.equals(repwd)) {
			//重复密码不正确
			System.out.println("两次密码不一致");
			Result<Object> result = new Result<>();
			result.setMessage("两次输入的密码不一致");
			return result;
		}
		
		User user1 = new User().setAccount(account).setPwd(newpwd);
		//修改密码
		userService.changePwd(user1);
		System.out.println("修改密码成功");
		Result<Object> result = new Result<>();
		result.setMessage("修改密码成功");
		return result;
		
	}
	//删除角色
	@GetMapping("/delbyuid")
	public Result<Object> delByUid(int uid) {
		userService.delByUid(uid);
		List<User> users = userService.findAllUser();
		Result<Object> result = new Result<>();
		result.setData(users);
		result.setMessage("删除角色成功");
		return result;
	}
}
